import 'package:flutter/material.dart';
import 'package:hiworld/category.dart';
import 'package:hiworld/dynamic.dart';
import 'package:hiworld/message.dart';
import 'package:hiworld/mine.dart';

class AppomePage extends StatefulWidget {
  @override
  _AppHomePageStae createState() => _AppHomePageStae();
}

class _AppHomePageStae extends State<StatefulWidget> {
  var _index = 0;

  List<Widget> _homeWidgets = [
    DynamicPage(),
    MessagePage(),
    CategoryPage(),
    MinePage(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('岛上码农'),
      ),
      body: IndexedStack(
        index: _index,
        children: _homeWidgets,
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _index,
        onTap: _onBottomNagigationBarTapped,
        items: [
          _getBottomNavItem(
              '动态', 'images/dynamic.png', 'images/dynamic-hover.png', 0),
          _getBottomNavItem(
              '消息', 'images/message.png', 'images/message-hover.png', 1),
          _getBottomNavItem(
              '分类浏览', 'images/category.png', 'images/category-hover.png',
              2),
          _getBottomNavItem(
              '个人中心', 'images/mine.png', 'images/mine-hover.png', 3),
        ],
      ),
    );
  }

  void _onBottomNagigationBarTapped(int index) {
    setState(() {
      _index = index;
    });
  }

  BottomNavigationBarItem _getBottomNavItem(String tiele, String normalIcon,
      String pressedIcon, int index) {
    return BottomNavigationBarItem(
      icon: _index == index ? Image.asset(
          pressedIcon,
          width: 32,
          height: 28,
      ) : Image.asset(
        normalIcon,
        width: 32,
        height: 28,
      ),
      label: tiele,
    );
  }
}
